DIM Statement ---------------------------------------------------------------------------- Action Declares a variable and allocates storage space. Syntax DIM -SHARED- variable-(subscripts)- -AS type- -, variable-(subscripts)- -AS type--... Remarks The following list describes the parts of the DIM statement. ----------------------------------------------------------------------------- Part Description ---------------------------------------------------------------------------- SHARED The optional SHARED attribute allows all procedures in a module to share arrays and simple variables. This differs from the SHARED statement, which affects only variables within a single SUB or FUNCTION procedure. variable A BASIC variable name. subscripts The dimensions of the array. Multiple dimensions can be declared. The subscript syntax is Part Description ---------------------------------------------------------------------------- declared. The subscript syntax is described below. AS type Declares the type of the variable. The type may be INTEGER, LONG, SINGLE, DOUBLE, STRING (for variable-length strings), STRING * length (for fixed-length strings), CURRENCY, or a user-defined type. The argument subscript in the DIM statement has the following form. - lower% TO- upper% -,- lower% TO- upper%-... The TO keyword provides a way to indicate both the lower and the upper bounds of an array's subscripts. The following statements are equivalent (if there is no OPTION BASE statement). DIM A(8,3) DIM A(0 TO 8, 0 TO 3) DIM A(8,0 TO 3) Subscripts can be negative. TO can be used to specify any range of subscripts between -32,768 and 32,767, inclusive. DIM A(-4 TO 10) DIM B(-99 TO -5,-3 TO 0) If you use an implicitly dimensioned array (that is, any array that has not been declared with the DIM statement), the maximum value of each subscript in the array is 10. If you use a subscript that is greater than the specified maximum and if run-time error checking is in effect, BASIC generates the error message Subscript out of range. Note Run-time error checking is in effect when. - You run a program from the QBX environment. - You have created an .EXE file in the QBX environment and you have selected the Run-Time Error Checking option in the Make EXE File dialog box. - You have compiled your program with BC using the -D option to select run-time error checking. The DIM statement initializes all elements of numeric arrays to zero and all the elements of string arrays to null strings. The fields of record variables are initialized to zero, including fixed-string fields. The maximum number of dimensions allowed in a DIM statement is 60. If you try to declare a dimension for an array variable with a DIM statement after you have referred to the array, BASIC generates the error message Array already dimensioned. It is good programming practice to put the required DIM statements at the beginning of a program, outside of any loops. Static and Dynamic Arrays How you declare an array also determines whether it is static (allocated when the program is translated) or dynamic (allocated when the program is run). ----------------------------------------------------------------------------- How array is declared Allocation ---------------------------------------------------------------------------- Declared first in a COMMON statement Dynamic Implicitly dimensioned arrays Static Dimensioned with numeric constants or CONST statement Static constants Dimensioned with variables as subscripts Dynamic The following list shows examples of different DIM statements and results. ----------------------------------------------------------------------------- Statement Result Statement Result ---------------------------------------------------------------------------- DIM A(0 TO 9) The array A is allocated as a static array if $DYNAMIC is not in effect. DIM A(MAXDIM) If MAXDIM is defined in a CONST statement, A is a static array. If MAXDIM is a variable, the array is a dynamic array and is allocated only when the program reaches the DIM statement. For more information about static and dynamic arrays, see Appendix B, "Data Types, Constants, Variables, and Arrays" in the Programmer's Guide. Note If the array size exceeds 64K, if the array is not dynamic, and if the -AH option was not used, BASIC may generate the error message Subscript out of range or Array too big. Reduce the size of the array or make the array dynamic and use the -AH command-line option. Type Declarations In addition to declaring the dimensions of an array, the DIM statement also can be used to declare the type of a variable. For example, the following statement declares the variable to be an integer, even though there is no type-declaration character or DEFINT statement. DIM NumberOfBytes AS INTEGER The DIM statement provides a way to declare specific variables to be records. In the following example, the variable TopCard is declared as a record variable. TYPE Card Suit AS STRING * 9 Value AS INTEGER END TYPE DIM TopCard AS Card You also can declare arrays of records. TYPE Card Suit AS STRING * 9 Value AS INTEGER END TYPE DIM Deck(1 TO 52) AS Card Note BASIC now supports the CURRENCY data type (type suffix @). This can be used in the AS type clause of the DIM statement. Also, BASIC now supports static arrays in user-defined types. BASICA BASICA executes a DIM statement when it encounters the statement in the program. The array is allocated only when the statement is executed, so all arrays in BASICA are dynamic. See Also ERASE, OPTION BASE, REDIM Example The following example finds and prints the maximum and minimum of a set of values. ' Declare the dimensions for an array to hold the values. CONST MAXDIM=20 DIM A(1 TO MAXDIM) ' Use DIM to set up two integer variables. Other variables are SINGLE. DIM NumValues AS INTEGER, I AS INTEGER ' Get the values. NumValues=0 PRINT "Enter values one per line. Type END to end." DO INPUT A$ IF UCASE$(A$)="END" OR NumValues>=MAXDIM THEN EXIT DO NumValues=NumValues+1 A(NumValues)=VAL(A$) LOOP ' Find the maximum and minimum values. IF NumValues>0 THEN Max=A(1) Min=A(1) FOR I=1 TO NumValues IF A(I)>Max THEN Max=A(I) IF A(I)